package com.rzcoding.controller;

import com.rzcoding.dto.base.ResultBean;
import com.rzcoding.dto.base.ResultConst;
import com.rzcoding.dto.resp.RolePermissionRespMo;
import com.rzcoding.entity.Role;
import com.rzcoding.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api")
public class AuthController {

    @Autowired
    private AuthService roleService;

    @GetMapping("/rights/{type}")
    public ResponseEntity findPermissions(@PathVariable String type){
        Object resp = roleService.findPermList(type);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.GET_ORIGHTSLIST_SUCC, resp), HttpStatus.OK);
    }

    @GetMapping("/roles")
    public ResponseEntity findRoleList(){
        List<RolePermissionRespMo> roles = roleService.findRoleList();
        return new ResponseEntity<>(ResultBean.ok(ResultConst.GET_ROLELIST_SUCC, roles), HttpStatus.OK);
    }

    @GetMapping("/roles/{id}/rights")
    public ResponseEntity findRole(@PathVariable Integer id){
        RolePermissionRespMo role = roleService.findRole(id);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.GET_ROLE_SUCC, role), HttpStatus.OK);
    }

    @PostMapping("/roles")
    public ResponseEntity addRole(Role role){
        roleService.add(role);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.ADD_ROLE_SUCC), HttpStatus.OK);
    }

    @PutMapping(value="/roles/{id}")
    public ResponseEntity updateRole(Role role, @PathVariable Integer id){
        role.setId(id);
        roleService.update(role);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.UPD_ROLE_SUCC), HttpStatus.OK);
    }

    @DeleteMapping(value = "/roles/{id}" )
    public ResponseEntity deleteRole(@PathVariable Integer id){
        roleService.delete(id);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.DEL_ROLE_SUCC), HttpStatus.OK);
    }

    @PutMapping(value="/roles/{id}/rights")
    public ResponseEntity updateRolePermission(@RequestParam(name = "rids") String rids, @PathVariable Integer id){
        roleService.updateRolePermission(id, rids);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.UPD_ROLEAUTH_SUCC), HttpStatus.OK);
    }

    @DeleteMapping(value="/roles/{id}/rights/{rightId}")
    public ResponseEntity delRolePermission(@PathVariable Integer id, @PathVariable Integer rightId){
        roleService.delRolePermission(id, rightId);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.DEL_ROLEAUTH_SUCC), HttpStatus.OK);
    }

    @GetMapping("/menus")
    public String findMenus(){
        return "{\"data\":[{\"id\":125,\"authName\":\"用户管理\",\"path\":\"users\",\"children\":[{\"id\":110,\"authName\":\"用户列表\",\"path\":\"users\",\"children\":[],\"order\":null}],\"order\":1},{\"id\":103,\"authName\":\"权限管理\",\"path\":\"rights\",\"children\":[{\"id\":111,\"authName\":\"角色列表\",\"path\":\"roles\",\"children\":[],\"order\":null},{\"id\":112,\"authName\":\"权限列表\",\"path\":\"rights\",\"children\":[],\"order\":null}],\"order\":2},{\"id\":101,\"authName\":\"商品管理\",\"path\":\"goods\",\"children\":[{\"id\":104,\"authName\":\"商品列表\",\"path\":\"goods\",\"children\":[],\"order\":1},{\"id\":115,\"authName\":\"分类参数\",\"path\":\"params\",\"children\":[],\"order\":2}],\"order\":3},{\"id\":102,\"authName\":\"订单管理\",\"path\":\"orders\",\"children\":[{\"id\":107,\"authName\":\"订单列表\",\"path\":\"orders\",\"children\":[],\"order\":null}],\"order\":4},{\"id\":145,\"authName\":\"数据统计\",\"path\":\"reports\",\"children\":[{\"id\":146,\"authName\":\"数据报表\",\"path\":\"reports\",\"children\":[],\"order\":null}],\"order\":5}],\"meta\":{\"msg\":\"获取菜单列表成功\",\"status\":200}}";
    }
}